package com.ht.controller;


import com.ht.core.AjaxResult;
import com.ht.entity.FlightRoute;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author guoyuxing
 * @date 2025/5/8 11:00
 */
@RestController
@RequestMapping("flightRoute/")
public class FlightRouteController {
    private List<FlightRoute> myStrs;

    public FlightRouteController(){
        myStrs = new ArrayList<>();
        myStrs.add(new FlightRoute(1001,"长沙-深圳","飞行单位1","长沙县","2025-05-01 08:30:00","1500米","3000米","500米","网格1","物流"));
        myStrs.add(new FlightRoute(1002,"长沙-北京","飞行单位2","芙蓉区","2025-05-03 09:10:00","2500米","4000米","700米","网格2","测绘"));
        myStrs.add(new FlightRoute(1003,"长沙-上海","飞行单位3","雨花区","2025-05-02 08:05:00","1800米","3300米","800米","网格3","内部"));
    }

    @GetMapping("getById")
    public AjaxResult getById(Integer id){
        Optional<FlightRoute> first = myStrs.stream().filter(s -> s.getId().equals(id)).findFirst();
        return first.isPresent() ? AjaxResult.success(first.get()) : AjaxResult.error("未找到ID"+id+"对应的数据！");
    }

    @GetMapping("getList")
    public AjaxResult getList(){
        return AjaxResult.success(myStrs);
    }

    @PostMapping("add")
    public AjaxResult add(@RequestBody FlightRoute flightRoute){
        return AjaxResult.success(myStrs.add(flightRoute));
    }

    @PostMapping("update")
    public AjaxResult update(@RequestBody FlightRoute flightRoute){
        for (int i = 0; i < myStrs.size(); i++) {
            if(myStrs.get(i).getId().equals(flightRoute.getId())){
                myStrs.set(i,flightRoute);
            }
        }
        return AjaxResult.success(myStrs);
    }

    @GetMapping("deleteById")
    public AjaxResult deleteById(Integer id){
        Optional<FlightRoute> first = myStrs.stream().filter(s -> s.getId().equals(id)).findFirst();
        return  first.isPresent() ? AjaxResult.success(myStrs.remove(first.get())) : AjaxResult.error("未找到ID"+id+"对应的数据！");
    }
}
